package com.cxyzy.demo.tween_anims

import android.os.Bundle
import android.view.View
import android.view.animation.Animation
import android.view.animation.TranslateAnimation
import androidx.appcompat.app.AppCompatActivity
import com.cxyzy.demo.R
import kotlinx.android.synthetic.main.activity_move.*

/**
 * 动画样例：移动控件
 */
class MoveActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_move)
        initView()
    }

    private fun initView() {
        button1.setOnClickListener { move(it, destBtn) }
    }

    private fun move(srcView: View, destView: View) {
        val animateTime = 2000L
        val yOffset = destView.top - srcView.top
        val xOffset = destView.left - srcView.left
        val animation = TranslateAnimation(0f, xOffset.toFloat(), 0f, yOffset.toFloat())

        animation.fillAfter = true
        animation.duration = animateTime
        animation.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationEnd(animation: Animation) {
                srcView.clearAnimation()
                srcView.visibility = View.INVISIBLE
            }

            override fun onAnimationStart(animation: Animation) {}
            override fun onAnimationRepeat(animation: Animation) {}
        })

        srcView.startAnimation(animation)
    }
}
